<template>
  <div class="AddOrUpdate_main add_container column_one">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <el-form-item label="企业管理员" prop="adminName">
          <el-input v-model="formData.adminName" disabled />
        </el-form-item>

        <el-form-item label="变更为" prop="newAdminId">
          <el-select v-model="formData.newAdminId" filterable placeholder="请选择" clearable>
            <el-option
              v-for="item in userOptions"
              :key="item.userId"
              :label="item.realName"
              :value="item.userId"
              clearable
            />
          </el-select>
        </el-form-item>
      </el-form>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { changeAdmin, list } from '@/api/system/user'
export default {
  name: 'AddOrUpdate',
  props: {
    layerid: {},
    defaultValue: {},
    id: {}
  },
  data() {
    return {
      formData: {
        adminName: undefined,
        newAdminId: undefined
      },
      rules: {
        newAdminId: [
          { required: true, message: '新企业管理员不能为空', trigger: 'change' }
        ]
      },
      userOptions: []
    }
  },
  created() {
    Object.keys(this.defaultValue).forEach(key => { // 给右边数据赋值
      this.formData[key] = this.defaultValue[key]
    })

    list({ status: 1 }).then(res => {
      this.userOptions = res.data.filter(item => item.userId != this.$store.state.user.userId)
    })
  },
  methods: {
    onSubmit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          changeAdmin(this.formData).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$layer.close(this.layerid) // 关闭弹框
            this.$parent.getList() // 调用父组件的刷新方法
          })
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>

</style>
